/**
 * 
 */
package cn.haona.game.simb.servlet.jsp.tag;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import cn.haona.game.simb.servlet.Define;
import cn.haona.game.simb.servlet.SessionHn;
import cn.haona.game.simb.wap.servlet.WebDefine;

/**
 * @author Administrator
 * 
 */
public class UrlHandleTag extends TagSupport {

	// 地址
	private String url;

	// //前缀
	// private String prefixUrl;
	// //Session_id
	// private String sessionID;

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public int doStartTag() throws JspException {

		return super.doStartTag();
	}

	public int doEndTag() throws JspException {

		// 请求对象
		HttpServletRequest request = (HttpServletRequest) pageContext
				.getRequest();

		// 应用网址前缀
		String strPrefix = null;
		if(WebDefine.WEB_APP_ROOT!=null)
		{
			strPrefix=WebDefine.WEB_APP_ROOT;
		}
		else
		{
			strPrefix = request.getScheme() + "://" + request.getServerName() + ":"
			+ request.getServerPort() + request.getContextPath();	
			WebDefine.WEB_APP_ROOT=strPrefix;
		}


		// 从请求字符串中取的SessionID
		String sessionID = request.getParameter(Define.SESSION_ID);
		// 如果没有
		if (sessionID == null) {
			// 从request.getAttribute取的对象
			SessionHn sessionHn = (SessionHn) request
					.getAttribute(Define.SESSION_ID);
			if (sessionHn != null) {
				sessionID = sessionHn.getId();
			}
			//如果还没有
			if (sessionID == null) {
				sessionID = "";
			}
		}

		//从请求字符串中取出OperateID
		String operateID=(String)request.getAttribute(WebDefine.OPERATE_KEY);
		
		// 输出
		JspWriter out = pageContext.getOut();

		String strOut = url;
		// 地址与参数分割位置
		int iCutIndex = -1;

		iCutIndex = strOut.indexOf("?");
		// 没有参数
		if (iCutIndex == -1) {
			strOut = strOut + "?" + Define.SESSION_ID + "=" + sessionID+"&amp;"+WebDefine.OPERATE_KEY+"="+operateID;
		}
		// 已有参数
		else {
			strOut = strOut + "&amp;" + Define.SESSION_ID + "=" + sessionID+"&amp;"+WebDefine.OPERATE_KEY+"="+operateID;
		}

		strOut = strPrefix + strOut;
		try {
			out.print(strOut);
		} catch (IOException e) {
			throw new JspException(e);
		}
		return super.doEndTag();
	}

}
